package com.example.children.Exception;

import com.example.children.Util.Tools;
import com.example.children.Model.ResponseModel;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice//类变成一个全局的异常处理器，可以自动扫描并捕获所有控制器类,@RestController 或 @Controller）中抛出的异常。
public class GlobalExceptionHandler   {    //封装管理其他异常的类

//    // 你也可以捕获其他类型的异常，比如一般的异常：
//    @ExceptionHandler(Exception.class)
//    public ResponseModel handleException(Exception ex) {
//        ResponseModel response = new ResponseModel(Tools.Fail, "服务器发生错误", null);
//        return  response;
//    }


    @ExceptionHandler(LoginException.class)
    public ResponseModel<String> handleCustomLoginException(LoginException ex) {
        // 处理CustomLoginException异常
        ResponseModel<String> response = new ResponseModel<>(Tools.Fail, "登录失败: " + ex.getMessage(), null);
        return response;
    }

    @ExceptionHandler(RegisterException.class)
    public ResponseModel<String> handleRegisterException(RegisterException ex) {
        // 处理CustomLoginException异常
        ResponseModel<String> response = new ResponseModel<>(Tools.Fail, "注册失败: " + ex.getMessage(), null);
        return response;
    }

    @ExceptionHandler(TokenException.class)
    public ResponseModel<String>  TokenException(TokenException ex) {
        // 处理TokenException异常(Token)
        ResponseModel<String> response = new ResponseModel<>(Tools.Fail, "Token过期或无效: " + ex.getMessage(), null);
        return response;
    }

    // 处理其他类型的异常
    @ExceptionHandler(Exception.class)
    public ResponseModel<String>  handleException(Exception ex) {
        ResponseModel<String> response = new ResponseModel<>(Tools.Fail, "服务器发生错误: " + ex.getMessage(), null);
        return response;
    }

    //出勤异常
    @ExceptionHandler(AttendanceException.class)
    public ResponseModel<String>  AttendanceException(AttendanceException ex) {
        ResponseModel<String> response = new ResponseModel<>(Tools.Fail, "出勤异常: " + ex.getMessage(), null);
        return response;
    }

    //日常行为异常
    @ExceptionHandler(BehaviorException.class)
    public ResponseModel<String>  BehaviorException(BehaviorException ex) {
        ResponseModel<String> response = new ResponseModel<>(Tools.Fail, "日常行为异常: " + ex.getMessage(), null);
        return response;
    }

    //体检异常
    @ExceptionHandler(HealthCheckException.class)
    public ResponseModel<String>  HealthCheckException(HealthCheckException ex) {
        ResponseModel<String> response = new ResponseModel<>(Tools.Fail, "体检异常: " + ex.getMessage(), null);
        return response;
    }

    //学生异常
    @ExceptionHandler(StudentException.class)
    public ResponseModel<String>  StudentException(StudentException ex) {
        ResponseModel<String> response = new ResponseModel<>(Tools.Fail, "学生异常: " + ex.getMessage(), null);
        return response;
    }

    //班级异常
    @ExceptionHandler(ClassException.class)
    public ResponseModel<String>  ClassException(ClassException ex) {
        ResponseModel<String> response = new ResponseModel<>(Tools.Fail, "班级信息异常: " + ex.getMessage(), null);
        return response;
    }

    //老师异常
    @ExceptionHandler(TeacherException.class)
    public ResponseModel<String>  TeacherException(TeacherException ex) {
        ResponseModel<String> response = new ResponseModel<>(Tools.Fail, "老师信息异常: " + ex.getMessage(), null);
        return response;
    }














}

